<?php
// +----------------------------------------------------------------------
// | Copyright (c) 齐力众信
// +----------------------------------------------------------------------
// | Author: 范思宇 
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台广告管理控制器
 */
class GustController extends AdminController {

    private $posterSpace;

    private $poster;

    public function _initialize(){

        parent::_initialize();
        $this->posterSpace = D('PosterSpace');
        $this->poster = D('Poster');

    }

    public function index(){

        $mode = C('DEVELOP_MODE');

        $list   = $this->lists('message');



        $this->assign('mode',$mode);

        $this->assign('_list',$list);
        
        $this->display();
    }

    public function add(){

        if(IS_POST){ //提交表单
        
            if(false !== $this->posterSpace->update()){

                $this->success('新增成功！', U('Poster/index'));

            } else {

                $error = $this->posterSpace->getError();
               
               $this->error(empty($error) ? '未知错误！' : $error);

            }//end if

        } else {
        
            $this->display('edit');
            
        }// end if

    }

    public function edit(){

        $id = I('id');

        if(IS_POST){ //提交表单
        
            if(false !== $this->posterSpace->update()){

                $this->success('编辑成功！', U('Poster/index'));

            } else {

                $error = $this->posterSpace->getError();
               
               $this->error(empty($error) ? '未知错误！' : $error);

            }//end if

        } else {

            /* 获取广告位信息 */
            $info = $id ? $this->posterSpace->info($id) : '';
    
            $this->assign('info', $info);
        
            $this->display();
            
        }// end if
    }

    public function call(){

        $id = I('id');

        $url = U('Home/Poster/showPoster',array('id'=>$id));

        $js = '<script language="javascript" src="'.$url.'"></script>';

        $this->assign('js', $js);
        
        $this->display();

    }

 
    public function listsIndex(){

        $space_id =  I('get.spaceid');


        $space_info = $this->posterSpace->info($space_id);

        $map['spaceid'] = $space_id;

        $map['lang'] = cookie('lang');
        if($space_id == 32 ){
            $model = D('Category');
            $map['pid'] = 0;
            $list = $this->lists($model,$map);
            // dump($list);
            $this->assign('_list',$list);
            $this->display('cateindex');
        }else{
            $list   = $this->lists('Poster',$map,'sort desc');

            $this->assign('spaceid',$space_id);

            $this->assign('name',$space_info['name']);

            $this->assign('_list',$list);

            $width = ($space_info['width'] >500) ? floor($space_info['width']/2) : $space_info['width'];

            $height = ($space_info['height'] >500) ? floor($space_info['height']/2) : $space_info['height'];


            $this->assign('w',$width);

            $this->assign('h',$height);
            $this->display();
        }
        
        
        

    }

    public function listsAdd(){

        $space_id =  I('spaceid');

        if(IS_POST){ //提交表单
        
            if(false !== $this->poster->update()){

                $this->success('新增成功！', U('Poster/listsIndex',array('spaceid'=>$space_id)));

            } else {

                $error = $this->poster->getError();
               
               $this->error(empty($error) ? '未知错误！' : $error);

            }//end if

        } else {

            $space_info = $this->posterSpace->info($space_id);

            $this->assign('name',$space_info['name']);

            $this->assign('spaceid',$space_id);
        
            $this->display('listsEdit');
            
        }// end if

    }


    public function listsEdit(){

        $space_id =  I('spaceid');
        $id =  I('id');

        if(IS_POST){ //提交表单
        
            if(false !== $this->poster->update()){

                $this->success('编辑成功！', U('Poster/listsIndex',array('spaceid'=>$space_id)));

            } else {

                $error = $this->poster->getError();
               
               $this->error(empty($error) ? '未知错误！' : $error);

            }//end if

        } else {

            /* 获取合作伙伴信息 */
            $info = $id ? $this->poster->info($id) : '';

            $space_info = $this->posterSpace->info($space_id);

            $this->assign('name',$space_info['name']);
    
            $this->assign('info', $info);

            $this->assign('spaceid',$space_id);
        
            $this->display();
            
        }// end if

    }

    /* 删除友情链接*/
    public function listsDel(){
        
        $id = array_unique((array)I('ids',0));

        $space_id =  I('spaceid');
    
        if(empty($id)){
            
            $this->error('参数错误!');
        }
         
        $map = array('id' => array('in', $id) );

        $list = $this->poster->where($map)->select();
        
        foreach($list as $key){

            $pic = get_cover($key['thumb']);
        
            D('Picture')->removePic($pic);

        }
        
        $res = $this->poster->where($map)->delete();
         
        if($res !== false){

            $this->success('删除广告成功！', U('Poster/listsIndex',array('spaceid'=>$space_id)));
                
        }else{
                    
            $this->error('删除广告失败！');
        }

    }

    public function cateEdit($id){

        if( empty($id) ){
            $this->error('参数错误');
        }
        $model = D('category');
        if(IS_POST){
            // $data = $model->create();
            $result = $model->update();
            if($result){
                $this->success('广告更新成功');
            }else{
                $this->error('广告修改失败');
            }
        }else{
            $category = $model->info($id);
            $this->assign('info',$category);
            $this->display();  
        }
        
    }
    public function del(){
       $id=$_GET['ids'];
        $model=M('message');
      if($model->delete($id)){
            $this->success("删除成功");
        }else {
            $this->error("删除失败");
        }
    }

}
?>
